package com.mall.order.services;

import com.mall.order.OrderDeleteService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.DeleteOrderRequest;
import com.mall.order.dto.DeleteOrderResponse;
import com.mall.order.dto.OrderDto;
import com.mall.shopping.constants.ShoppingRetCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Component
@Service
@Transactional
public class OrderDeleteServiceImpl implements OrderDeleteService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    StockMapper stockMapper;



    @Override
    public DeleteOrderResponse orderDelete(DeleteOrderRequest request) {

        DeleteOrderResponse response = new DeleteOrderResponse();

        try {
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());


            Example example = new Example(OrderItem.class);
            example.createCriteria().andEqualTo("orderId",request.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            for (OrderItem orderItem : orderItems) {
                int delete = orderItemMapper.deleteByPrimaryKey(orderItem);
                if(delete != 1) {
                    response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
                    response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
                    return response;
                }
                //如果是未付款状态,还库存,其他状态不更新库存
                if(order.getStatus() == 0) {
                    Example example1 = new Example(Stock.class);
                    example1.createCriteria().andEqualTo("itemId", orderItem.getItemId());
                    List<Stock> stocks = stockMapper.selectByExample(example1);
                    for (Stock stock : stocks) {
                        stock.setLockCount(stock.getLockCount() - orderItem.getNum());
                        stock.setStockCount(stock.getStockCount() + orderItem.getNum());
                        int update = stockMapper.updateByStock(stock);
                        if(update != 1){
                            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
                            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
                            return response;
                        }
                    }
                }
            }
            int delete = orderMapper.deleteByPrimaryKey(order);
            if(delete != 1) {
                response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
                response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
                return response;
            }
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e) {
            response.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }
}
